package com.controller;

import com.mapper.TeamMapper;
import com.pojo.Student;
import com.pojo.Team;
import com.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 小组
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    TeamMapper tm;

    /**
     * 创建小组
     * @param name 组名
     * @param leader_id 组长 open_id
     * @param invitation_code 邀请码
     * @param introduce 小组介绍
     */
    @RequestMapping("/create")
    public Map<String,Object> create(String name,String leader_id,String invitation_code,String introduce) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(name)) {
            code = 500;
            msg = "name 不能为空";
        } else if (Tool.isNull(leader_id)) {
            code = 501;
            msg = "leader_id 不能为空";
        } else if (Tool.isNull(invitation_code)) {
            code = 502;
            msg = "invitation_code 不能为空";
        } else if (Tool.isNull(introduce)) {
            code = 503;
            msg = "小组介绍不能为空";
        } else if (tm.getStudentById(leader_id) == null) {
            code = 504;
            msg = "组长 open_id 不存在";
        } else if (tm.getStudentTeam(leader_id) != null) {
            code = 505;
            msg = "组长已有小组";
        } else {
            Integer rows = tm.createTeam(leader_id,name,invitation_code,introduce);
            if (rows == 1) tm.joinTeam(tm.getTeamByLeader(leader_id).getId(), leader_id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 加入小组
     * @param open_id 用户 open_id
     * @param team_id 小组 id
     * @param invitation_code 验证码
     */
    @RequestMapping("/join")
    public Map<String,Object> join(String open_id,Integer team_id,String invitation_code) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (open_id == null) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (team_id == null) {
            code = 501;
            msg = "team_id 不能为空";
        } else if (invitation_code == null) {
            code = 502;
            msg = "invitation_code 不能为空";
        } else if (tm.getStudentById(open_id) == null) {
            code = 503;
            msg = "open_id 不存在";
        } else if (tm.getStudentById(open_id).getTeam_id() != null) {
            code = 504;
            msg = "用户已加入其他小组";
        } else {
            Team team = tm.getTeamById(team_id);
            if (team == null) {
                code = 505;
                msg = "小组不存在";
            } else if (!team.getCode().equals(invitation_code)) {
                code = 505;
                msg = "邀请码错误";
            } else {
                tm.joinTeam(team_id,open_id);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取小组信息
     * @param id 小组 id
     */
    @RequestMapping("/getInfo")
    public Map<String,Object> getInfo(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else {
            Team team = tm.getTeamById(id);
            if (team == null) {
                code = 501;
                msg = "小组不存在";
            } else {
                List<Student> list = tm.getTeamMember(id,team.getLeader_id());
                list.add(0,tm.getStudentById(team.getLeader_id()));
                for (Student item : list) {
                    if (item.getOpen_id().equals(team.getLeader_id())) {
                        team.setLeader_name(item.getNickname());
                    }
                    item.setPassword(null);
                }
                team.setTeam(list);
                map.put("data",team);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除成员
     * @param team_id 小组 id
     * @param open_id 成员 open_id
     */
    @RequestMapping("/delMember")
    public Map<String,Object> delMember(Integer team_id,String open_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (team_id == null) {
            code = 500;
            msg = "team_id 不能为空";
        } else if (open_id == null) {
            code = 501;
            msg = "open_id 不能为空";
        } else if (tm.getTeamById(team_id) == null) {
            code = 502;
            msg = "team_id 不存在";
        } else {
            Student student = tm.getStudentById(open_id);
            if (student == null) {
                code = 503;
                msg = "open_id 不存在";
            } else if (!student.getTeam_id().equals(team_id)) {
                code = 504;
                msg = "该用户不是小组成员";
            } else {
                tm.joinTeam(null,open_id);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 报名课题
     * @param open_id 组长 open_id
     * @param topic_id 课题 id
     */
    @RequestMapping("/sign")
    public Map<String,Object> sign(String open_id,Integer topic_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(open_id)) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (topic_id == null) {
            code = 501;
            msg = "课题 id 不能为空";
        } else if (tm.getStudentById(open_id) == null) {
            code = 502;
            msg = "open_id 不存在";
        } else {
            Team team = tm.getTeamByLeader(open_id);
            if (team == null) {
                code = 503;
                msg = "小组不存在";
            } else tm.sign(topic_id,team.getId());
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 解散小组
     * @param id 小组 id
     * @param open_id 组长 id
     */
    @RequestMapping("/dissol")
    public Map<String,Object> dissol(Integer id,String open_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "小组 id 不能为空";
        } else if (Tool.isNull(open_id)) {
            code = 501;
            msg = "组长 id 不能为空";
        } else {
            Team team = tm.getTeamById(id);
            if (team == null) {
                code = 502;
                msg = "小组不存在";
            } else if (!team.getLeader_id().equals(open_id)) {
                code = 503;
                msg = "你无权解散该小组";
            } else {
                tm.dissol(id);
                tm.delTeam(id);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改小组信息
     * @param id 小组 id
     * @param name 小组名
     * @param introduce 小组介绍
     * @param _code 邀请码
     */
    @RequestMapping("/editInfo")
    public Map<String,Object> editInfo(Integer id,String name,String introduce,String _code) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "小组 id 不能为空";
        } else if (tm.getTeamById(id) == null) {
            code = 501;
            msg = "小组不存在";
        } else if (Tool.isNull(name)) {
            code = 502;
            msg = "小组名不能为空";
        } else if (Tool.isNull(introduce)) {
            code = 503;
            msg = "小组介绍不能为空";
        } else if (Tool.isNull(_code)) {
            code = 504;
            msg = "验证码不能为空";
        } else tm.editInfo(id, name, introduce, _code);

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }
}
